//集成cropper插件 - 裁剪插件
const image = document.getElementById('image');
const cropper = new Cropper(image, {
    aspectRatio: 1,
    preview: $('.img-preview')
});

//点击按钮，触发文件域的点击行为，弹出窗口让用户选择文件
$(".select").on('click', function () {
    $('#file').click();
})
//文件域的改变事件
$('#file').on('change', function (ev) {
    var imgURL = URL.createObjectURL(ev.target.files[0]);
    cropper.replace(imgURL)
})
//确定点击事件
$('.sure').on('click', function () {
    var canvas = cropper.getCroppedCanvas({
        width: 100,
        height: 100
    });
    var base64Img = canvas.toDataURL('image/jpg');
    var argStr = `avatar=${encodeURIComponent(base64Img)}`;
    upAvatorAPI(argStr, function (res) {
        console.log(res);
        window.parent.userInfo();
    })
})

getUserInForAPI({}, function (res) {
    console.log(res)
    cropper.replace(res.data.data.user_pic);
})